package svc.elib.socnet;

import java.io.IOException;
import java.util.LinkedList;

import svc.elib.db.Author;
import svc.elib.db.Database;
import svc.elib.db.Paper;

/**
 * Digital library network constructor
 * 
 * @author svc
 * @date 7.6.2013
 */
public class SocConstructor {

	private Database db;
	
	private Net net;
	
	public SocConstructor(Database db) {
		this.db = db;
		net = new Net(db.getName());
		construct();
	}
		
	private void construct() {
		LinkedList<Author> authors = db.getAuthors();
		for (int i = 0; i < authors.size(); i++) {
			Author a = authors.get(i);
			net.createNode(a);
		}
		
		LinkedList<Paper> papers = db.getPapers();
		for (int i = 0; i < papers.size(); i++) {
			Paper p = papers.get(i);
			LinkedList<Author> pAuthors = p.getAuthors();
			for (int k = 1; k < pAuthors.size(); k++) {
				for (int j = 0; j < k; j++) {
					Author src = pAuthors.get(j);
					Author dst = pAuthors.get(k);
					net.createLink(src, dst);
				}
			}
		}
	}
	
	public Net getNet() {
		return net;
	}
	
	
	public static void main(String[] args) 
		throws IOException
	{
		Database db = new Database("eLibData.csv", 1932, 2011);
		System.out.println(db.info());
		SocConstructor soc = new SocConstructor(db);
		Net net = soc.getNet();
		net.printInfo();
		
		System.out.println("SW coeff: " + net.getSW() + ", another: " + net.getSW2());
		
		ConnectedComponents comp = new ConnectedComponents(net);
		comp.resolveComponents();
		comp.componentsInfo();
		
		/*
		//EvolutionarySnapshots es = new EvolutionarySnapshots(net);
		//es.info();
		
		Net largestComponent = comp.getComponents().get(0);
		//largestComponent.printInfo();
		//es = new EvolutionarySnapshots(largestComponent);
		//es.info();
		
		Net secondComponent = comp.getComponents().get(1);
		Net thirdComponent = comp.getComponents().get(2);
		Net fourthComponent = comp.getComponents().get(3);
		Net fifthComponent = comp.getComponents().get(4);
	
		System.out.println("Authors in largest component... ");
		largestComponent.dumpAuthors();
		
		System.out.println("\n\nAuthors in second largest component... ");
		secondComponent.dumpAuthors();
		
		System.out.println("\n\nAuthors in third largest component... ");
		thirdComponent.dumpAuthors();
		
		System.out.println("\n\nAuthors in fourth largest component... ");
		fourthComponent.dumpAuthors();
		
		System.out.println("\n\nAuthors in fifth largest component... ");
		fifthComponent.dumpAuthors();
		*/
	}
}
